{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "74abde73-2161-415d-b956-3f7766ea35e2",
   "metadata": {},
   "source": [
    " # python流程与执行\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e5c3f87c-c8b1-496e-a00c-c1470ae3e025",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eceeec79980541b8806be13b23bb2d71",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(width=1000)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=1000,height=500)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "786593cb-4ae0-4912-9dcd-5be313747ac5",
   "metadata": {},
   "outputs": [],
   "source": [
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "71a1b25e-8577-404e-ac92-b1181bf2058c",
   "metadata": {},
   "outputs": [],
   "source": [
    "myTS.delay(0)\n",
    "n=10\n",
    "colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']\n",
    "t = turtle.Turtle(myTS)\n",
    "t.shape(\"turtle\")\n",
    "for _ in range(25):\n",
    "    with(hold_canvas(myCanvas)):\n",
    "        t.pencolor(colors[_%6])\n",
    "    for _ in range(4):\n",
    "        t.forward(n)\n",
    "        t.left(90)\n",
    "    t.left(15)\n",
    "    n+=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5f5f268b-c7fb-47ff-8835-0a25b3927040",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "for _ in range(2):\n",
    "  for _ in range(360):\n",
    "      bob.forward(a)\n",
    "      bob.left(1)\n",
    "  a+=0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "218371f3-1dcd-4f12-abbe-dec9052e3662",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "59e15c1e1952459f82ec4d3f4d741e24",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=250, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "比赛开始! 总距离200米\n",
      "兔子开始打盹了，加油啊龟龟，现在就是你反超的时候了\n",
      "兔子开始打盹了，加油啊龟龟，现在就是你反超的时候了\n",
      "比赛结束! 是乌龟赢了!\n"
     ]
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "import random\n",
    "import time\n",
    "from ipyturtle3 import hold_canvas\n",
    "random.seed(time.time())\n",
    "myCanvas = turtle.Canvas(width=500, height=250)\n",
    "display(myCanvas)\n",
    "myTS = turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()\n",
    "myTS.bgcolor(\"lightgreen\")\n",
    "\n",
    "pen = turtle.Turtle(myTS)\n",
    "pen.hideturtle()\n",
    "pen.penup()\n",
    "pen.goto(-100, 100)\n",
    "pen.pendown()\n",
    "pen.goto(-100, -100)\n",
    "pen.penup()\n",
    "pen.goto(100, 100)\n",
    "pen.pendown()\n",
    "pen.goto(100, -100)\n",
    "\n",
    "bob = turtle.Turtle(myTS)\n",
    "hare = turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "hare.shape(\"circle\")  #好像没有兔子模型\n",
    "bob.penup()\n",
    "hare.penup()\n",
    "bob.goto(-100, 50)\n",
    "hare.goto(-100, -50)\n",
    "bob.pendown()\n",
    "hare.pendown()\n",
    "\n",
    "myTS.delay(50)\n",
    "\n",
    "bob_0, hare_0 = 0, 0\n",
    "b_speed = random.randint(3,5)*0.2\n",
    "h_speed = random.randint(4,6)\n",
    "lazy_time = 0\n",
    "\n",
    "print(\"比赛开始! 总距离200米\")\n",
    "for x in range(1000):\n",
    "    if bob_0 >= 200 or hare_0 >= 200:\n",
    "        break\n",
    "    bob.forward(b_speed)\n",
    "    bob_0 += b_speed\n",
    "    if lazy_time > 0:\n",
    "        lazy_time -= 1\n",
    "    else:\n",
    "        hare.forward(h_speed)\n",
    "        hare_0 += h_speed\n",
    "        if hare_0 - bob_0 >= 50 and random.random() < 0.8:\n",
    "            lazy_time = random.randint(80, 90)\n",
    "            print(f\"兔子开始打盹了，加油啊龟龟，现在就是你反超的时候了\")\n",
    "            \n",
    "if bob_0 > hare_0:\n",
    "    result = \"是乌龟赢了!\"\n",
    "elif bob_0 < hare_0:\n",
    "    result = \"是兔子赢了!\"\n",
    "else: result = \"平局!\"\n",
    "print(f\"比赛结束! {result}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ef91195-5808-4a6d-bae0-e596acce9004",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
